﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

class PuzzleManager
{
    private List<PuzzleGroup> list;
    public List<PuzzleGroup> PuzzleGroups
    {
        get { return list; }
        set { list = value; }
    }

    ContentManager content;

    public PuzzleManager(ContentManager content)
    {
        list = new List<PuzzleGroup>();
        list.Add(new PuzzleGroup(-1));
        //Console.WriteLine("puzzle manager criado");
        this.content = content;
    }

    public void CheckPuzzles(int[,] objectsMatriz, int[,] eventsMatriz, bool solved)
    {
        list.Clear();
        list.Add(new PuzzleGroup(-1));

        //checa todos os eventos
        for (int k = 0; k < 40; k++)
        {
            //se existe um evento
            if (eventsMatriz[0, k] >= 0)
            {
                //Console.WriteLine("    checando puzzle objeto: " + objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]]);
                //PuzzleObject Object = new PuzzleObject((ObjectKind)objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]], eventsMatriz[1, k], eventsMatriz[2, k], false);

                if (objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.TouchingButton
                    || objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.WallUp
                    || objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.StoneButton)
                {
                    //Console.WriteLine("    checando puzzle objeto: " + objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]]);

                    PuzzleObject Object = null;

                    bool found = false;

                    foreach (PuzzleObject obj in GameSession.ObjectsList)
                    {
                        if (eventsMatriz[1, k] == obj.XPosition && eventsMatriz[2, k] == obj.YPosition)
                        {
                            found = true;
                            Object = obj;
                        }
                    }

                    if (!found)
                    {
                        if (solved && objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.WallUp)
                        {
                            Object = new PuzzleObject((ObjectKind)objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]], eventsMatriz[1, k], eventsMatriz[2, k], true);
                        }
                        else
                        {
                            Object = new PuzzleObject((ObjectKind)objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]], eventsMatriz[1, k], eventsMatriz[2, k], false);
                        }
                        
                    }

                    bool newGroupFlag = true;

                    //verifica se jah existe um grupo pra ele
                    foreach (PuzzleGroup group in list)
                    {
                        if (eventsMatriz[0, k] == group.GroupID)
                        {
                            //Console.WriteLine("         adicionando ao grupo: " + eventsMatriz[0, k]);
                            Object.AddGroup(group);
                            group.AddObject(Object);
                            if (objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.StoneButton)
                                group.StoneGroup = true;
                            newGroupFlag = false;
                        }
                    }
                    if (newGroupFlag)
                    {
                        //Console.WriteLine("         criando grupo: " + eventsMatriz[0, k]);
                        PuzzleGroup newGroup = new PuzzleGroup(eventsMatriz[0, k]);
                        Object.AddGroup(newGroup);
                        newGroup.AddObject(Object);
                        if (objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.StoneButton)
                            newGroup.StoneGroup = true;
                        list.Add(newGroup);
                    }
                }
                else
                {
                    PuzzleGroup group = list.ElementAt(0);

                    if (objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.PushingStone)
                    {
                        PuzzleObject Object = new PuzzleObject((ObjectKind)objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]], eventsMatriz[1, k], eventsMatriz[2, k], false);

                        //Console.WriteLine("    checando puzzle objeto: " + objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] + " " + group.GroupID);

                        Object.CanCollide = true;
                        group.AddObject(Object);
                    }
                    else
                    {
                        if (objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.Tower)
                        {
                            Tower Object = GetTower(objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] , eventsMatriz[1, k], eventsMatriz[2, k]);

                            if (eventsMatriz[0, k] > 0 && eventsMatriz[0, k] <=4)
                                Object.ShotDirection.Add( (Direction) eventsMatriz[0, k]);

                            group.AddObject(Object);                          
                        }
                        else
                            if (objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.TrapTimerDown)
                            {
                                group.AddObject(new Trap(TrapKind.Timer, eventsMatriz[1, k], eventsMatriz[2, k]));
                            }
                            else
                                if (objectsMatriz[eventsMatriz[1, k], eventsMatriz[2, k]] == (int)ObjectKind.TrapDown)
                                {
                                    group.AddObject(new Trap( (TrapKind) eventsMatriz[0, k], eventsMatriz[1, k], eventsMatriz[2, k]));
                                }
                    }
                }
            }
        }

    }

    private Tower GetTower(int kind, int posx, int posy)
    {
        foreach(PuzzleObject obj in GameSession.ObjectsList)
        {
            if (posx == obj.XPosition && posy == obj.YPosition)
                return (Tower)obj;
        }

        return new Tower((ObjectKind)kind, posx, posy);
    }
}